package com.hqyj.onlineExamination.modules.common.controller;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

//可以处理没有进入控制层的异常
@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class MyBasicErrorController implements ErrorController {

    @Override
    public String getErrorPath() {
        return null;
    }

    @RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
    public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
        //获取状态码
        HttpStatus status = getStatus(request);
        //如果状态码是404
        if (status.value() == 404) {
            return new ModelAndView("forward:/common/404");
            //或return "forward:/common/404";
        }
        return new ModelAndView("forward:/common/500");
        //return "forward:/common/500";
    }

    @RequestMapping
    public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        HttpStatus status = getStatus(request);
        map.put("status", status.value());
        map.put("message", status.getReasonPhrase());
        if (status == HttpStatus.NOT_FOUND) {
            map.put("data", "/common/404");
        } else {
            map.put("data", "/common/505");
        }
        return new ResponseEntity<Map<String, Object>>(map, status);
    }

    protected HttpStatus getStatus(HttpServletRequest request) {
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if (statusCode == null) {
            return HttpStatus.INTERNAL_SERVER_ERROR;
        } else {
            try {
                return HttpStatus.valueOf(statusCode);
            } catch (Exception var4) {
                return HttpStatus.INTERNAL_SERVER_ERROR;
            }
        }
    }
}